'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async register() { // 注册
    const res = await this.ctx.service.user.register(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async login() { // 登录
    const res = await this.ctx.service.user.login(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async generateInviteCode() { // 生成邀请码
    const checkResult = this.ctx.helper.checkLogin(this);
    if (checkResult.result) {
      const res = await this.ctx.service.user.generateInviteCode(this.ctx.user);
      this.ctx.body = res;
      this.ctx.status = res.status;
    } else {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
    }
  }

  async checkCode() { // 校验邀请码
    const res = await this.ctx.service.user.checkCode(this.ctx.request.body.code);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async profile() { // 生成用户信息json文件
    const checkResult = this.ctx.helper.checkLogin(this);
    if (checkResult.result) {
      const res = await this.ctx.service.user.profile();
      this.ctx.body = res;
      this.ctx.status = res.status;
    } else {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
    }
  }

  async uploadAvatar() { // 上传头像
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.common.upload('avatar');
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async upload() { // 上传图片
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.common.upload();
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async saveUserInfo() { // 保存用户信息的修改
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.user.saveUserInfo(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async homeInfo() { // 用户主页信息
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.user.homeInfo(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async focus() { // 关注作者
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.user.focus(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async cancelFocus() { // 取消关注作者
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.user.cancelFocus(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async recommendAuthor() { // 推荐作者列表
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.user.recommendAuthor(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }
  
}

module.exports = UserController;
